package com.example.studyktdemo.ui.activity

import androidx.fragment.app.FragmentTransaction
import com.example.studyktdemo.R
import com.example.studyktdemo.base.BaseActivity
import com.example.studyktdemo.bean.model.bean.TabEntity
import com.example.studyktdemo.databinding.ActivityMainBinding

import com.example.studyktdemo.ui.fragment.DiscoveryFragment
import com.example.studyktdemo.ui.fragment.HomeFragment
import com.example.studyktdemo.ui.fragment.HotFragment
import com.example.studyktdemo.ui.fragment.MineFragment
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import java.util.ArrayList

class MainActivity : BaseActivity<ActivityMainBinding>() {
    private val mTitles = arrayOf("每日精选", "发现", "热门", "我的")

    // 未被选中的图标
    private val mIconUnSelectIds = intArrayOf(
        R.mipmap.ic_home_normal,
        R.mipmap.ic_discovery_normal,
        R.mipmap.ic_hot_normal,
        R.mipmap.ic_mine_normal
    )

    // 被选中的图标
    private val mIconSelectIds = intArrayOf(
        R.mipmap.ic_home_selected,
        R.mipmap.ic_discovery_selected,
        R.mipmap.ic_hot_selected,
        R.mipmap.ic_mine_selected
    )

    private var mHomeFragment: HomeFragment? = HomeFragment()
    private var mDiscoveryFragment: DiscoveryFragment? = DiscoveryFragment()
    private var mHotFragment: HotFragment? = HotFragment()
    private var mMineFragment: MineFragment? = MineFragment()

    private var mTabEntities = ArrayList<CustomTabEntity>()

    //默认为0，选中第一个
    private var mIndex = 0

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun initView() {
        //初始化标签页
        initTab()

        binding.tabLayout.currentTab = mIndex
        switchFragment(mIndex)
    }

    private fun initTab() {
        (0 until mTitles.size)
            .mapTo(mTabEntities) {
                TabEntity(
                    mTitles[it],
                    mIconSelectIds[it],
                    mIconUnSelectIds[it]
                )
            }

        //为tab赋值
        binding.tabLayout.setTabData(mTabEntities)
        binding.tabLayout.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                //切换fragment
                switchFragment(position)
            }

            override fun onTabReselect(position: Int) {

            }
        })
    }

    /**
     * 切换fragment
     */
    private fun switchFragment(position: Int) {
        val startTransaction = supportFragmentManager.beginTransaction()
        hideFragments(startTransaction)
        //隐藏当前fragment
        //mCurFragment?.let { startTransaction.hide(it) }
        when (position) {
            //首页
            /* 0 -> mHomeFragment?.let {
                 startTransaction.show(it)
             } ?: HomeFragment().let {
                 mHomeFragment = it
                 if (it != null) {
                     startTransaction.add(R.id.fl_container, it, "mHomeFragment")
                 }
             }

             1 -> mDiscoveryFragment?.let {
                 startTransaction.show(it)
             } ?: DiscoveryFragment().let {
                 mDiscoveryFragment = it
                 if (it != null) {
                     startTransaction.add(R.id.fl_container, it, "mDiscoveryFragment")
                 }
             }

             2 -> mHotFragment?.let {
                 startTransaction.show(it)
             } ?:HotFragment().let {
                 mHotFragment = it
                 if (it != null) {
                     startTransaction.add(R.id.fl_container, it, "mHotFragment")
                 }
             }

             3 -> mMineFragment?.let {
                 startTransaction.show(it)
             } ?: MineFragment().let {
                 mMineFragment = it
                 if (it != null) {
                     startTransaction.add(R.id.fl_container, it, "mMineFragment")
                 }
             }
             else ->{

             }*/
            0 -> {
                mHomeFragment?.let {
                    startTransaction.show(it)
                    mHomeFragment = it
                    if (!mHomeFragment!!.isAdded) {
                        startTransaction.add(R.id.fl_container, it, "mHomeFragment")
                    }
                }
            }
            1 -> {
                mDiscoveryFragment?.let {
                    startTransaction.show(it)
                    mDiscoveryFragment = it
                    if (!mDiscoveryFragment!!.isAdded) {
                        startTransaction.add(R.id.fl_container, it, "mDiscoveryFragment")
                    }
                }
            }
            2 -> {
                mHotFragment?.let {
                    startTransaction.show(it)
                    mHotFragment = it
                    if (!mHotFragment!!.isAdded) {
                        startTransaction.add(R.id.fl_container, it, "mHotFragment")
                    }
                }
            }
            3 -> {
                mMineFragment?.let {
                    startTransaction.show(it)
                    mMineFragment = it
                    if (!mMineFragment!!.isAdded) {
                        startTransaction.add(R.id.fl_container, it, "mMineFragment")
                    }
                }
            }
        }
        mIndex = position
        binding.tabLayout.currentTab = mIndex
        startTransaction.commit()
    }

    /**
     * 隐藏fragment
     */
    private fun hideFragments(startTransaction: FragmentTransaction) {
        mHomeFragment?.let { startTransaction.hide(it) }
        mDiscoveryFragment?.let { startTransaction.hide(it) }
        mHotFragment?.let { startTransaction.hide(it) }
        mMineFragment?.let { startTransaction.hide(it) }
    }
}